import Util from "../../common/Util"
import Api from "../../common/Api"

const FETCH_BLOGS="LIST/FETCH_BLOGS";
const FETCH_BLOGS_SUCCESS="LIST/FETCH_BLOGS_SUCCESS";
const FETCH_BLOGS_FAILURE="LIST/FETCH_BLOGS_FAILURE";

const REMOVE_BLOG="LIST/REMOVE_BLOG";
const REMOVE_BLOG_SUCCESS="LIST/REMOVE_BLOG_SUCCESS";
const REMOVE_BLOG_FAILURE="LIST/REMOVE_BLOG_FAILURE";

const CREATE_BLOG="DETAIL/CREATE_BLOG";
const CREATE_BLOG_SUCCESS="DETAIL/CREATE_BLOG_SUCCESS";
const CREATE_BLOG_FAILURE="DETAIL/CREATE_BLOG_FAILURE";

const UPDATE_BLOG="DETAIL/UPDATE_BLOGS";
const UPDATE_BLOG_SUCCESS="DETAIL/UPDATE_BLOG_SUCCESS";
const UPDATE_BLOG_FAILURE="DETAIL/UPDATE_BLOG_FAILURE";


//const CHANGE_PAGE="CHANGE_PAGE";

const SET_BLOG="DETAIL/SET_BLOG"
const CHANGE_BLOG="DETAIL/CHANGE_BLOG"

const initialState = {
  blogs:[],
//  showList:true,
  blog:{},
  user:{}
};

export default function reducer(state=initialState,action={}){
  const {type,payload}=action;

  switch (type) {
    case FETCH_BLOGS_SUCCESS:
      return Object.assign({}, state, {
        blogs: payload.blogs,
        user:payload.user
      });
    case UPDATE_BLOG_SUCCESS:
      return Object.assign({}, state, {
        blogs: payload
      });
    case REMOVE_BLOG_SUCCESS:
      return Object.assign({}, state, {
        blogs: payload
      });
    // case CHANGE_PAGE:
    //   return Object.assign({},state,{
    //     showList:payload.showList,
    //     blog:payload.blog,
    //   })
    case SET_BLOG:
      return Object.assign({},state,{
        blog:payload
      })
    case CHANGE_BLOG:
      return Object.assign({},state,{
        blog:{ ...state.blog, ...payload }
      })
    case CREATE_BLOG_SUCCESS:
      return state;
    default:
      return state;
  }
}

// export function showList(){
//   return {
//     type:CHANGE_PAGE,
//     payload:{showList:true,blog:{}}
//   }
// }
//
// export function showDetail(blog){
//   return {
//     type:CHANGE_PAGE,
//     payload:{showList:false,blog:blog}
//   }
// }

export function fetchBlogs(){
    return dispatch => {
     Util.get(Api.blog_list,(data)=>{
      dispatch({type:FETCH_BLOGS_SUCCESS,payload:data})
    });
  }
}

export function createBlog(blog){
  return dispatch=>{
     Util.post(Api.blog_save,blog,(data)=>{
      dispatch({type:CREATE_BLOG_SUCCESS,payload:data})
    });
  }
}

export function updateBlog(blog){
  return dispatch=>{
     Util.post(Api.blog_save,blog,(data)=>{
      dispatch({type:UPDATE_BLOG_SUCCESS,payload:data})
    });
  }
}

export function removeBlog(id){
  const url=[Api.blog_delete,id].join("/");
  return dispatch=>{
     Util.delete(url,(data)=>{
      dispatch({type:REMOVE_BLOG_SUCCESS,payload:data})
    });
  }
}

export function setBlog(postId) {
  // return (dispatch, getState) => {
  //   const { blogs } = getState().blog;
  //
  //   const blog = blogs.filter(obj => obj.id === parseInt(postId))[0];
  //
  //   if (!blog) return;
  //
  //   dispatch({
  //     type: SET_BLOG,
  //     payload: blog
  //   });
  // };
  const url=[Api.blog_delete,postId].join("/");
  return dispatch=>{
     Util.get(url,(data)=>{
      dispatch({type:SET_BLOG,payload:data})
    });
  }
}

export function changeBlog(blog){
  return{
    type:CHANGE_BLOG,
    payload:blog
  }
}
